package test.serverframe.armc.server.manager.service.exec;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import test.serverframe.armc.server.manager.dao.mapper.InvokeRecordMapper;
import test.serverframe.armc.server.manager.dao.mapper.TaskEnterInvokeMapper;
import test.serverframe.armc.server.manager.dao.mapper.TaskInvokeMapper;
import test.serverframe.armc.server.manager.domain.InvokeRecord;
import test.serverframe.armc.server.manager.domain.TaskEnterInvoke;
import test.serverframe.armc.server.manager.domain.TaskInvoke;
import test.serverframe.armc.server.manager.service.exec.params.TaskInvokeExecutorParams;

@Service("executorEnterService")
public class ExecutorEnterService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private TaskInvokeMapper invokeMapper;
	
	@Autowired
	private TaskEnterInvokeMapper taskEnterInvokeMapper;

	@Autowired
	private InvokeRecordMapper invokeRecordMapper;
	
    @Transactional
    public void executorEnter(String fkId, String isReStart) {
        InvokeRecord waitRecord = new InvokeRecord();
        //计数参数
        AtomicInteger taskTotal = new AtomicInteger(0);
        AtomicInteger successTaskTotal = new AtomicInteger(0);
        TaskInvoke taskInvoke = null;
        try {
			List<TaskEnterInvoke> list = taskEnterInvokeMapper.selectByTaskInvokeId(fkId);
	        taskInvoke = invokeMapper.selectByPrimaryKey(fkId);
	        
	        if (taskInvoke != null) {
		        //初始化调度记录的执行状态
		        String invokeRocordId = UUID.randomUUID().toString();
		        waitRecord.setRecordId(invokeRocordId);
		        waitRecord.setTaskInvokeId(fkId);
		        waitRecord.setStatus(Constants.EXECUTING);
		        waitRecord.setStartTime(new Date());
		        waitRecord.setTaskSuccessTotal(String.valueOf(0));
		        waitRecord.setTaskTotal(String.valueOf(list.size()));
		        waitRecord.setNodeArr(taskInvoke.getNodeArr());
		        
		        TaskInvokeExecutorParams params = new TaskInvokeExecutorParams();
		        params.setTaskInvokeId(fkId);
		        params.setInvokeRecordId(invokeRocordId);
		        params.setFailedStrategy(taskInvoke.getFailedStrategy());
		        params.setSuccessTaskTotal(successTaskTotal);
		        params.setTaskTotal(taskTotal);
		        params.setIsReStart(Boolean.valueOf(isReStart));
		        params.setExpectedTaskTotal(list.size());
		        BaseExecutor.ThreadPool.execute(new TaskInvokeExecutor(params));
	        } else {
	        	logger.warn("调度不存在，停止执行， 调度id：" + fkId);
	        }
		} catch (Exception e) {
			logger.error("执行调度失败，原因：" + e);
		} finally {
			if (taskInvoke != null) {
				invokeRecordMapper.insert(waitRecord);
			}
		}
        
    }
}
